﻿using System;
using System.Data.SqlServerCe;
using System.Data;

namespace CSCDBookConverter
{
    class SQLCEIDhelper
    {
        public static void SetPrimaryKey(SqlCeConnection cnn,
                                SqlCeRowUpdatedEventArgs e)
        {

            // If this is an INSERT operation...
            if (e.Status == UpdateStatus.Continue && e.StatementType == StatementType.Insert)
            {
                DataColumn[] pk = e.Row.Table.PrimaryKey;
                /// and a primary key PK column exists...
                if (pk != null && pk.Length == 1)
                {
                    //Set up the post-update query to fetch new @@Identity
                    SqlCeCommand cmdGetIdentity = new SqlCeCommand("SELECT @@IDENTITY", cnn);

                    //Execute the command and set the result identity value to the PK
                    e.Row[pk[0]] = Convert.ToInt32(cmdGetIdentity.ExecuteScalar());
                    e.Row.AcceptChanges();

                }
            }
        }

    }
}
